package com.ourteam.education.api;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.education.dao.CampusDAOQueryBean;
import com.ourteam.education.dao.CourseTypeDAOQueryBean;
import com.ourteam.education.domain.CampusBean;
import com.ourteam.education.domain.CourseTypeBean;
import com.ourteam.education.service.ICampusManageService;
import com.ourteam.frame.controller.AbstractCRUDServiceController;

@Controller
@CrossOrigin
@RequestMapping("/api/EDUCATION/campus")
public class CampusManageAPI extends AbstractCRUDServiceController<CampusDAOQueryBean, CampusSearchBean, CampusBean> {

	@Autowired
	private ICampusManageService campusManageService;

	@Override
	protected CampusDAOQueryBean newQueryBean() {
		return new CampusDAOQueryBean();
	}

	@Override
	protected int queryCount(CampusDAOQueryBean queryBean) throws Exception {
		return campusManageService.queryCampusCount(queryBean);
	}

	@Override
	protected CampusBean[] queryData(CampusDAOQueryBean queryBean) throws Exception {
		return campusManageService.queryCampuss(queryBean);
	}

	@Override
	protected void deleteData(long[] ids) throws Exception {

		campusManageService.deleteCampuss(ids);

	}

	@Override
	protected CampusBean getDataById(long id) throws Exception {
		return campusManageService.getCampusById(id);
	}

	@Override
	protected void saveData(CampusBean dataBean) throws Exception {

		if (dataBean.getId() == 0) {
			campusManageService.addNewCampus(dataBean);
		} else {
			campusManageService.modifyCampus(dataBean);
		}

	}

	@RequestMapping("/list")
	@ResponseBody
	public CampusBean[] list(final @RequestParam(required = false) String campusName) throws Exception {

		CampusDAOQueryBean searchBean = new CampusDAOQueryBean();

		CampusDAOQueryBean.Criteria criteria = searchBean.createCriteria();

		criteria.andStatusEqualTo("available");

		if (StringUtils.isNoneBlank(campusName)) {
			criteria.andCampusNameLike(campusName);
		}

		return campusManageService.queryCampuss(searchBean);
	}

}
